package day09;

public class ArraysUtil {
	// getmax
	public int getMax(int arr[]) {
		int max = arr[0];
		for (int i = 1; i < arr.length; i++) {
			if (max < arr[i])max = arr[i];
		}
		return max;

	}
	// getmin
	public int getMin(int arr[]) {
		int min = arr[0];
		for (int i = 0; i < arr.length; i++) {
			min = (min > arr[i])? arr[i]:min;
		}
		return min;

	}
	// sum
	public int getSum(int arr[]){
		int sum = arr[0];
		for (int i = 1; i < arr.length; i++) {
			sum += arr[i];
		}
		return sum;
	}
	// avg
	public double getAvg(int arr[]){
		double avg = (getSum(arr)+0.0)/arr.length;
		return avg;
	}
	
	// sort 
	public void sort(int arr[]){
		quicksort(arr,0,arr.length-1);
		
	}
	
	public void swap(int[] arr,int i,int j){
		if(arr[i]==arr[j])return;
		arr[i]^=arr[j];
		arr[j]^=arr[i];
		arr[i]^=arr[j];
	}
	
	public void quicksort(int arr[],int l,int r){
		if (l>=r) return; 
		int left = l,right= r;
		while (left < right) {
			if (arr[right]>=arr[l]){
				--right;
				continue;
				}
			else if (arr[left]<=arr[l]){
				++left;
				continue;
			}
			
			if(left < right)swap(arr,left,right);
			--right;
			
		}
		swap(arr,l,left);
		quicksort(arr,l,left-1);
		quicksort(arr,left+1,r);
	
	}
	
	// copy
	public int[] copy(int arr[]){
		int[] arrcopy = new int[arr.length];
		for (int i = 0; i < arrcopy.length; i++) {
			arrcopy[i] = arr[i];
		}
		return arrcopy;
	}

	// getindex
	public int getindex(int arr[],int parm) {
		 for (int i = 0; i < arr.length; i++) {
			if(parm == arr[i]){
				return i;
			}
		 }
		 return -1;
	}
	
	// print
	public void print(int arr[]){
		System.out.print("[");
		for (int i = 0; i < arr.length; i++) {
			if(i==arr.length-1)System.out.println(arr[i]+"]");
			else System.out.print(arr[i]+",");
		}
		
	}
}
